package com.version.manager.shiro;


import com.version.manager.service.UserService;
import com.version.manager.util.JWTUtil;
import com.version.manager.vo.JwtAuthenticationRequest;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

@Component
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JWTToken;
    }

    /**
     * 验证用户
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("------身份认证--------");
        String token=(String)authenticationToken.getCredentials();
        JwtAuthenticationRequest jwtInfo= JWTUtil.getJwtInfo(token);
        if(jwtInfo==null||!JWTUtil.verify(token,jwtInfo)){
            throw new AuthenticationException("token认证失败");
        }
        String password=userService.getPassword(jwtInfo.getCompanyCode(),jwtInfo.getUserName());
        if(!jwtInfo.getPassWord().equals(password)){
            throw new AuthenticationException("密码错误");
        }
        return new SimpleAuthenticationInfo(token,token,"MyRealm");
    }

    /**
     * 验证权限
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        System.out.println("————权限认证————");
        Set<String> test = new TreeSet<>();
        test.add("view");
        //String username = JWTUtil.getJwtInfo(principals.toString());
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(test);
//        //获得该用户角色
//        String role = userService.getRole(username);
//        //每个角色拥有默认的权限
//        String rolePermission = userService.getRolePermission(username);
//        //每个用户可以设置新的权限
//        String permission = userService.getPermission(username);
//        Set<String> roleSet = new HashSet<>();
//        Set<String> permissionSet = new HashSet<>();
//        //需要将 role, permission 封装到 Set 作为 info.setRoles(), info.setStringPermissions() 的参数
//        roleSet.add(role);
//        permissionSet.add(rolePermission);
//        permissionSet.add(permission);
//        //设置该用户拥有的角色和权限
//        info.setRoles(roleSet);
//        info.setStringPermissions(permissionSet);
        return info;
    }


}
